package threads.wait;

public class WaitTest {
    private final Object lock = new Object();
    Thread waitThread;
    Thread notifyThread;
    private volatile boolean waited = false;

    public void waits() {
        waitThread = new Thread(new Runnable() {
            @Override
            public void run() {
                synchronized (lock) {
                    waited = true;
                    System.out.println("I will be waited");
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("I have be notified");
                }
            }
        });
        waitThread.start();
    }

    public void notifys() {
        notifyThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (!waited) {
                    System.out.println("1");
                }
                synchronized (lock) {
                    System.out.println("I will notify the Thread of lock's monitor");
                    lock.notify();
                }
            }
        });
        notifyThread.start();
    }

    public static void main(String[] args) {
        WaitTest waitTest = new WaitTest();
        waitTest.notifys();
        waitTest.waits();

    }
}
